module FSM_UI(clock, resetN, lock, CDA_done, cx_in, cy_in, colour_in, radius_in, run_CDA, ui_CX, ui_CY, ui_Colour, ui_Radius);

	input clock, 
			resetN, 
			lock, 
			CDA_done;
			
	input [8:0] cx_in;
	input [7:0] cy_in;
	input [2:0] colour_in;
	input [4:0] radius_in;
			
	output run_CDA;
	
	//UI Registers
	output reg [8:0]ui_CX;
	output reg [7:0]ui_CY;
	output reg [2:0]ui_Colour;
	output reg [4:0]ui_Radius;

	reg [2:0] ps;
	reg [2:0] ns;
	
	localparam S0 = 1'b0, S1 = 1'b1;
	
	// Main FSM transitions
	always@(*)
		case(ps)
			S0    : if(lock) ns = S1; else ns = S0;
			S1    : if(~lock && CDA_done) ns = S0; else ns = S1;
			default: ns = 1'bx;
		endcase
		
	// FSM state updating
	always@(posedge clock, negedge resetN) begin
		if(~resetN)
			ps <= S0;
		else
			ps <= ns;			
	end
	
	always@(posedge clock) begin
		if(~run_CDA) begin
			ui_CX <= cx_in;
			ui_CY <= cy_in;
			ui_Colour <= colour_in;
			ui_Radius <= radius_in;
		end
		
	end
	// Control output assignment 
	assign run_CDA = ps == S1;

endmodule 